#ifndef _LOADCONTROL_H_
#define _LOADCONTROL_H_

#include <winsock2.h>
#include <interfacelib/window/panel.h>
#include <interfacelib/frame/frame.h>
#include <utillib/script/xmlobject.h>
#include <utillib/stl/map.h>
#include <utillib/util/singleton.h>

#define		MAX_CONTROL_NAME		128

#define		FOREGROUND_COLOR	0
#define		BACKGROUND_COLOR	1
#define		CARET_COLOR			2

#define		MAX_ADDITIONAL_PROPERY		3

namespace interfacelib
{
	namespace Window
	{
		class LoadControl
		{
		public:
			LoadControl();
			virtual ~LoadControl();

		public:
			struct Property
			{
				bool enable;
				DWORD value;
			};
			struct ControlProperty
			{
				DWORD typeCode;
				POINT pos;
				SIZE size;
				DWORD attribute;
				char *name;
				char *function;
				char resourceName[ _MAX_PATH ];
				char ballonLineImage[ _MAX_PATH ];
				char ballonBottomImage[ _MAX_PATH ];
				char tooltipImage[ _MAX_PATH ];
				char soundFile[ _MAX_PATH ];

				POINT tooltipPos;
				SIZE tooltipMargin;
				char *tooltip;

				Property additionProperty[ MAX_ADDITIONAL_PROPERY ];

				interfacelib::Window::Window *control;

				bool isReadOnly;
				bool isMultiLine;
				bool isWordWrap;
				bool isDisplayPosition;
				TiXmlNode *xmlNode;
				DWORD textFormat;
				DWORD strokeColor;
				char passwordChar;

				ControlProperty()
				{
					typeCode = interfacelib::Window::Window::WINDOW;
					pos.x = -1;
					pos.y = -1;
					size.cx = -1;
					size.cy = -1;
					control = NULL;
					attribute = interfacelib::Window::Window::VISIBLE;

					name = NULL;
					function = NULL;
					tooltip = NULL;

					ZeroMemory( resourceName, _MAX_PATH );
					ZeroMemory( ballonLineImage, _MAX_PATH );
					ZeroMemory( ballonBottomImage, _MAX_PATH );
					ZeroMemory( tooltipImage, _MAX_PATH );
					ZeroMemory( soundFile, _MAX_PATH );
					ZeroMemory( resourceName, _MAX_PATH );
					ZeroMemory( resourceName, _MAX_PATH );

					tooltipPos.x = -1;
					tooltipPos.y = -1;
					tooltipMargin.cx = 0;
					tooltipMargin.cy = 0;

					isReadOnly = false;
					isMultiLine = false;
					isWordWrap = false;
					strokeColor = 0;
					passwordChar = 0;

					isDisplayPosition = false;
					textFormat = DT_NOCLIP;

					xmlNode = NULL;
					for ( int i = 0; i < MAX_ADDITIONAL_PROPERY; i++ )
						additionProperty[ i ].enable = false;
				};
				~ControlProperty()
				{
					SAFE_DELETE( name );
					SAFE_DELETE( function );
					SAFE_DELETE( tooltip );
				};
			};
			ControlProperty *getControlProperty( char *name );
			interfacelib::Window::Window *getControl( char *name , DWORD windowType );

			void clearProperty();

			void loadFile( char *filename , interfacelib::Window::Panel *parent );
			void loadFile( char *filename , char *name, interfacelib::Window::Panel *parent );
			void loadChild( char *name, interfacelib::Window::Panel *parent );

			void setLinkFunctionPanel( interfacelib::Window::Panel *parent );
			void setLinkFunctionFrame( interfacelib::Frame::Frame * frame );

			interfacelib::Window::Window *createControl( char *name , interfacelib::Window::Window *window );
		protected:
			interfacelib::Window::Window *createControl( ControlProperty *controlProperty , interfacelib::Window::Window *window );
			interfacelib::Window::Window *createControl( ControlProperty *controlProperty );

		protected:

			void loadControl( interfacelib::Window::Panel *parent );
			bool parsingControlProperty( ControlProperty *controlProperty );
			

			DWORD getControlType( const char *controlType );
			bool getControlPos( const char *name, const char *value , ControlProperty *controlProperty );
			bool getControlAttribute( const char *name, const char *value , ControlProperty *controlProperty );
			bool getControlImage( const char *name, const char *value , ControlProperty *controlProperty );
			bool getControlAddtionInfo( const char *name, const char *value , ControlProperty *controlProperty );

			void setAddtionProperty( ControlProperty *controlProperty );

			bool isTrue( const char *str );
			
			interfacelib::Window::Window *createBaloonBox( ControlProperty *controlProperty );
			interfacelib::Window::Window *createButton( ControlProperty *controlProperty );
			interfacelib::Window::Window *createEditBox( ControlProperty *controlProperty );
			interfacelib::Window::Window *createMenu( ControlProperty *controlProperty );
			interfacelib::Window::Window *createScrollWithString( ControlProperty *controlProperty );

			template < class _Ty >
			_Ty *createWindow( ControlProperty *controlProperty )
			{
				_Ty *window = new _Ty;
				window->create( controlProperty->resourceName, controlProperty->pos.x, controlProperty->pos.y, controlProperty->attribute );

				return ( window );
			}

			utillib::stl::MapWithString < ControlProperty * > m_mapControlProperty;

			void loadMenuItem( interfacelib::Window::Menu *menu );

		private:
			interfacelib::Frame::Frame *m_frame;
			interfacelib::Window::Panel *m_panel;

			utillib::script::XMLObject m_xmlObject;
			char m_currentPath[ _MAX_PATH + 1 ];
		};
	};
};

#endif